package myarrays;

import java.util.Arrays;

/**
 *
 * @author Denisov Sergey
 */
public class MyArrays {

    public static double[] notexciding(double[] data, double min, double max) {
        double[] var = data;
        for (int i = 0; i < data.length; i++) {
            if (var[i] < min) {
                var[i] = min;
            }
            if (var[i] > max) {
                var[i] = max;
            }
        }
        return var;
    }

    public static double FindMax(double[][] data) {
        double[] row = data[0];
        Arrays.sort(row);
        double var = row[0];
        for (double[] data1 : data) {
            row = data1;
            Arrays.sort(row);
            if (var < row[row.length - 1]) {
                var = row[row.length - 1];
            }
        }
        return var;
    }

    public static double FindMax(double[] data) {
        Arrays.sort(data);
        return data[data.length - 1];
    }

    public static double FindMin(double[][] data) {
        double[] row = data[0];
        Arrays.sort(row);
        double var = row[row.length - 1];
        for (double[] data1 : data) {
            row = data1;
            Arrays.sort(row);
            if (var > row[0]) {
                var = row[0];
            }
        }
        return var;
    }

    /**
     * @param array - double[][] input array
     * @param i - integer position of column is need to be taken from array
     * @return double[]
     */
    public static double[] takecolumn(double[][] array, int i) {
        double[] var = new double[array.length];

        for (int j = 0; j < var.length; j++) {
            var[j] = array[j][i];
        }
        return var;
    }

    /**
     * @param array - double[][] input array
     * @param i - integer position of row is need to be deleted
     * @return double[][] array
     */
    public static double[][] deleterow(double[][] array, int i) {
        double[][] var = new double[array.length - 1][array[0].length];
        int a = 0;
        for (int j = 0; j < array.length; j++) {
            if (j != i) {
                var[a] = array[j];
                a++;
            }
        }
        return var;
    }

    /**
     *
     * @param array
     * @param i
     * @return
     */
    public static double[][] deletecolumn(double[][] array, int i) {
        return transpose(deleterow(transpose(array), i));
    }

    /**
     * @param array - double[][] input array
     * @return double[][] transposed array
     */
    public static double[][] transpose(double[][] array) {
        double[][] var = new double[array[0].length][array.length];
        int a = 0;
        for (int i = 0; i < var.length; i++) {
            for (int j = 0; j < var[0].length; j++) {
                var[i][j] = array[j][i];
            }
        }
        return var;
    }

    public static double average(double[] data) {
        double var = 0;
        for (int i = 0; i < data.length; i++) {
            var = var + data[i];
        }
        var = var / data.length;
        return var;
    }
}
